﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iAnywhere.Data.SQLAnywhere;
using Oracle.DataAccess.Client;
using System.Configuration;
using Syncer;
using System.Globalization;
using System.IO;
using System.Data;



namespace SybaseTest
{
    class Nd_Performance_Collector
    {


        static void Main(string[] args)
        {
            try
            {
                Log.OpenLog();
                string bppmConn = string.Empty;
                string oracleConn = string.Empty;


                foreach (var Key in ConfigurationManager.AppSettings.Keys)
                {
                    if (Key.ToString() == "福建省烟草专卖局")
                    {
                        bppmConn = ConfigurationManager.AppSettings[Key.ToString()];

                    }
                    else if (Key.ToString() == "unify")
                    {
                        oracleConn = ConfigurationManager.AppSettings["unify"];

                    }
                }

                List<ndPerformanceEntity> ndpe = new List<ndPerformanceEntity>();

                try
                {
                    ndpe = LoadEgEvent(bppmConn);

                }
                catch (Exception ex)
                {
                    Log.Exception("", ex);
                }

                try
                {
                    LoadAlertTable(oracleConn, ndpe);

                }
                catch (Exception ex)
                {
                    Log.Exception("", ex);
                }
            }
            finally { Log.CloseLog(); }
        }
        static List<ndPerformanceEntity> LoadEgEvent(string bppmStr)
        {

            List<ndPerformanceEntity> ndpe = new List<ndPerformanceEntity>();
            using (SAConnection conn = new SAConnection(bppmStr))
            {
                conn.Open();
                string sql = string.Empty;


                sql = @"select mc_tool_sev,status,mc_parameter,mc_parameter_value,severity,msg,mc_object_uri,mc_service,MC_HOST_ADDRESS,MC_ORIGIN_CLASS,MC_OBJECT_OWNER,MC_OBJECT,MC_LONG_MSG
                    from dba.eg_event_view
                    where status='OPEN' and eventclass='ALARM' ";



                Log.Info("Now is get egevent");

                SACommand cmd = new SACommand(sql, conn);
                SADataReader rd = cmd.ExecuteReader();


                while (rd.Read())
                {
                    ndpe.Add(new ndPerformanceEntity
                    {
                        mc_tool_sev = rd["mc_tool_sev"].ToString(),
                        status = rd["status"].ToString(),
                        mc_parameter = rd["mc_parameter"].ToString(),
                        mc_parameter_value = rd["mc_parameter_value"].ToString(),
                        severity = rd["severity"].ToString(),
                        msg = rd["msg"].ToString(),
                        mc_object_uri = rd["mc_object_uri"].ToString(),
                        mc_service = rd["mc_service"].ToString(),
                        MC_HOST_ADDRESS = rd["MC_HOST_ADDRESS"].ToString(),
                        MC_ORIGIN_CLASS = rd["MC_ORIGIN_CLASS"].ToString(),
                        MC_OBJECT_OWNER = rd["MC_OBJECT_OWNER"].ToString(),
                        MC_OBJECT = rd["MC_OBJECT"].ToString(),
                        MC_LONG_MSG = rd["MC_LONG_MSG"].ToString(),
                        


                    });
                }
                Log.Info("Now is load list");
            }

            return ndpe;
        }




        static void LoadAlertTable(string unifyshowStr, List<ndPerformanceEntity> ndpe)
        {
            using (OracleDataAccess oc = new OracleDataAccess())
            {


                oc.Open();

          string sql6 = string.Empty;
    sql6 = "delete from alert";
    oc.ExecuteReader(sql6);
 
      Log.Info("delete alert:" + sql6);

                //将list中的数据插入到event1中

             foreach (var event1 in ndpe)
                {
                    try
                    {

                        String mc_tool_sev = event1.mc_tool_sev;
                        String status = event1.status;
                        String mc_parameter = event1.mc_parameter;
                        String mc_parameter_value = event1.mc_parameter_value;
                        String severity = event1.severity;
                        String msg = event1.msg;
                        String mc_object_uri = event1.mc_object_uri;
                        String mc_service = event1.mc_service;
                        String MC_HOST_ADDRESS = event1.MC_HOST_ADDRESS;
                        String MC_ORIGIN_CLASS = event1.MC_ORIGIN_CLASS;
                        String MC_OBJECT_OWNER = event1.MC_OBJECT_OWNER;
                        String MC_OBJECT = event1.MC_OBJECT;
                        String MC_LONG_MSG = event1.MC_LONG_MSG;
                       



                        string sql4 = string.Empty;
   
                        sql4 = @"select device.id from device where device.IDENTIFY='"+MC_HOST_ADDRESS+"'  and device.OBJECT ='"+MC_OBJECT_OWNER+"'  and device.TYPE='服务器' union all select device.id from device where device.IDENTIFY='"+MC_HOST_ADDRESS+"' and device.TYPE='"+MC_ORIGIN_CLASS+"' and device.OBJECT ='"+MC_OBJECT_OWNER+"' and device.SUBOBJECT='"+MC_OBJECT+"' and device.TYPE='数据库' union all select device.id from device where device.IDENTIFY='"+MC_HOST_ADDRESS+"' and device.TYPE='"+MC_ORIGIN_CLASS+"' and device.OBJECT ='"+MC_OBJECT_OWNER+"' and device.SUBOBJECT='"+MC_OBJECT+"' and device.TYPE='中间件' ";
                       oc.ExecuteReader(sql4);
                     
                        object id= oc.ExecuteScalar(sql4);

                        if (null != id)
                        {
                            string sql5 = string.Empty;
                            sql5 = @"insert into alert (alert.eventid,alert.deviceid,alert.status,alert.parameter,alert.value,alert.severity,alert.msg,alert.ticketid,alert.ticketstatus,alert.TIMESTAMP,alert.CREATETIME,alert.LASTMODIFIEDTIME)
                           values ('" + mc_tool_sev + "', " + id + ",'" + status + "','" + mc_parameter + "','" + mc_parameter_value + "',2,'" + msg + "','" + mc_object_uri + "','" + mc_service + "',to_date('" + DateTime.Parse(MC_LONG_MSG).ToString("yyyy-MM-dd HH-mm-ss") + "','yyyy-mm-dd hh24-mi-ss'),to_date('" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "','yyyy-mm-dd hh24-mi-ss'),to_date('" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "','yyyy-mm-dd hh24-mi-ss'))";

                            oc.ExecuteReader(sql5);
                            Log.Info("insert into alert:" + sql5);
                        }
                        else
                        {
                            Log.Info("deviceID is null");
                            
                        }

                        
                    }
                    catch (Exception ex)
                    {
                        Log.Exception("", ex);
                        Log.CloseLog();
                     
                    }
                   
                }
            }
            
        }
        
    }
}


